<?php
namespace app\index\controller;

use app\index\model\UserModel;
use think\Controller;
use think\Request;
use org\Verify;
use think\Db;

class Login extends Controller
{
    public function index()
    {
        return view('login/index');
    }

    public function dologin(Request $request)
    {
        require ("Redis.php");
        $post = $request->post();
        $username = $post['username'];
        $pass = $_POST['password'];
        $id = $redis->get("username:".$username);
        if(!empty($id)){
            $password = $redis->hget("user:".$id,"password");
            if($pass == $password){
                $auth = md5(time().$username.rand());
                $redis->set("auth:".$auth,$id);
                setcookie("auth",$auth,time()+86400);
            }
        }

        $username = input("param.username");
        $password = input("param.password");
        $password = md5($password);

        $result = $this->validate(compact('username', 'password'), 'UserValidate');
        if(true !== $result){
            return json(msg(-1, '', $result));
        }

        $userModel = new UserModel();
        $hasUser = $userModel->findUserByName($username);
        if(empty($hasUser)){
            $this->error('用户不存在','index/login/index');
        }

        if($password != $hasUser['password']){
            $this->error('密码错误','index/login/index');
        }

        if(1 != $hasUser['status']){
            $this->error('该账户已经被禁用','index/login/index');
        }
        session('username', $username);
        session('id', $hasUser['id']);

        $this->success('登录成功','index/index/dologin');
    }

    public function loginOut()
    {
        session('username', null);
        session('id', null);
        $this->redirect(url('index/index/index'));
    }

    public function checkVerify()
    {
        $verify = new Verify();
        $verify->imageH = 32;
        $verify->imageW = 100;
        $verify->length = 4;
        $verify->useNoise = false;
        $verify->fontSize = 14;
        return $verify->entry();
    }

    public function tellogin()
    {
        return view('login/tellogin');
    }

    public function dotellogin()
    {
        require ("Redis.php");
        $tel = $_POST['tel'];
        $pass = $_POST['password'];
        $id = $redis->get("tel:".$tel);
        if(!empty($id)){
            $password = $redis->hget("user:".$id,"password");
            if($pass == $password){
                $auth = md5(time().$tel.rand());
                $redis->set("auth:".$auth,$id);
                setcookie("auth",$auth,time()+86400);
            }
        }

        $tel = input("param.tel");
        $password = input("param.password");
        $password = md5($password);


        $result = $this->validate(compact('tel', 'password'), 'UseValidate');
        if(true !== $result){
            return json(msg(-1, '', $result));
        }

        $userModel = new UserModel();
        $hasUser = $userModel->findUserByNam($tel);
        if(empty($hasUser)){
            $this->error('电话号码不存在','index/login/index');
        }

        if($password != $hasUser['password']){
            $this->error('密码错误','index/login/index');
        }

        if(1 != $hasUser['status']){
            $this->error('该账户已经被禁用','index/login/index');
        }

        $u = Db::field('username')
            ->table(['wb_buser'])
            ->where('tel = '.$tel)
            ->find();
        $username = implode("",$u);
        session('username', $username);
        session('id', $hasUser['id']);

        $this->success('登录成功','index/index/dologin');

    }

}




